// pages/children_detail/children_detail.js
import { getData } from "../mine/function";

const db = wx.cloud.database();
const _ = db.command;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    donorInfo: "",

    //new
    searchKeyWord: "",
    donatedChildren: [
      { id: 1, name: '张三', location: '云南', characteristic: '1' },
      { id: 2, name: '王五', location: '云南', characteristic: '1' },
      { id: 3, name: '李四', location: '云贵州', characteristic: '2' },
      { id: 4, name: '宋八', location: '云南', characteristic: '1' },
      { id: 5, name: '张五', location: '云南', characteristic: '1' },
      { id: 6, name: '赵七', location: '云南', characteristic: '1' },
      { id: 7, name: '朱六', location: '云南', characteristic: '1' }
    ],
    studentsShow: [],
    // filteredResults: []
    // search: ''
  },



  // ---------------------原生---------------------
  onChange(e) {
    this.setData({
      search: e.detail,
    });
    console.log(this.data.search);
  },

  // ---------------------搜索模块---------------------
  onSearch(event) {
    const that = this
    const searchKeyWord = event.detail  // 获取搜索关键词
    const donatedChildren = that.data.donatedChildren  // 获取 donatedChildren 数组
    console.log("donatedChildren", donatedChildren)

    // 使用 filter 方法进行筛选
    const filteredResults = donatedChildren.filter(child => {
      return child.name.includes(searchKeyWord) ||
        child.province.includes(searchKeyWord) ||
        child.tag.includes(searchKeyWord);
    });

    // 将筛选后的结果返回给前端展示
    that.setData({
      studentsShow: filteredResults
    })
  },

  // ---------------------utils---------------------
  async init() {
    var res = await getData("ourOx68K-yzP1JGUbwnM9FAEJ8hA");
    console.log(res);
    this.setData({
      donorInfo: res,
      donatedChildren: res.students,
      studentsShow: res.students,
    })
    // console.log("donorInfo", this.data.donorInfo)
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.init()

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.init()

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})